package com.kmzx.one.project.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kmzx.one.common.exceptions.ParameterNullException;
import com.kmzx.one.common.utils.UUIDGenerator;
import com.kmzx.one.project.entity.UserInvite;
import com.kmzx.one.project.entity.UserRankBuy;
import com.kmzx.one.project.entity.WxUser;
import com.kmzx.one.project.mapper.RankTabMapper;
import com.kmzx.one.project.mapper.UserInviteMapper;
import com.kmzx.one.project.mapper.UserRankBuyMapper;
import com.kmzx.one.project.mapper.UserRankMapper;
import com.kmzx.one.project.mapper.WxUserMapper;
import com.kmzx.one.project.others.common.UserRankDetailInfo;
import com.kmzx.one.project.others.common.VIPRank;
import com.kmzx.one.project.others.dso.UserRankDso;
import com.kmzx.one.project.others.dto.FatherInfo;
import com.kmzx.one.project.others.dto.MateOutInfo;
import com.kmzx.one.project.others.dto.UserMateOutInfo;
import com.kmzx.one.project.others.dto.UserRankInfo;
import com.kmzx.one.project.others.dto.UserRankMoreInfo;
import com.kmzx.one.project.others.dto.UserRankOutInfo;
import com.kmzx.one.project.service.UserRanksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class UserRanksServiceImpl implements UserRanksService {


    @Autowired
    UserRankMapper userRankMapper;
    @Autowired
    UserRankBuyMapper userRankBuyMapper;

    @Autowired
    RankTabMapper rankTabMapper;

    @Autowired
    WxUserMapper wxUserMapper;

    @Autowired
    UserInviteMapper userInviteMapper;

    @Override
    public UserRankInfo findUserRankTabs(String userId) {
        List<UserRankDso> tabs = userRankMapper.selectAllRankTabs();
        UserRankInfo rankTabs = userRankMapper.selectUserRankTabs(userId);
        if (rankTabs != null) {
            for (int i = 0; i < tabs.size(); i++) {
                if (tabs.get(i).getRankId().equals(rankTabs.getRankId()) && i != tabs.size() - 1) {
                    rankTabs.setNextName(tabs.get(i + 1).getRankName());
                    break;
                }
            }
            ArrayList<VIPRank> ranks = new ArrayList<>();
            for (int i = 0; i < tabs.size(); i++) {
                VIPRank rank = new VIPRank();
                rank.setName(tabs.get(i).getRankName());
                rank.setMoney(tabs.get(i).getRankPay());
                rank.setRankNum(tabs.get(i).getRankNum());
                ranks.add(rank);
//                if (tabs.get(i).getRankNum() != 0 && rankTabs.getRankNo() < tabs.get(i).getRankNum()) {
//                }
            }
            rankTabs.setRankList(ranks);
            rankTabs.setUserId(userId);
            Double seaRankRate = rankTabs.getSeaRankRate();
            if (seaRankRate == null) {
                rankTabs.setSeaRankRate(1.0);
            }
            BigDecimal rankPay = rankTabs.getRankPay();
            BigDecimal growth = rankTabs.getGrowth();
            BigDecimal toBuy = rankPay.subtract(growth);
            rankTabs.setToBuy(toBuy);
            return rankTabs;
        }
        return null;
    }

    @Override
    public List<UserRankMoreInfo> findTopRankUser(String userId, Integer pageNum, Integer pageSize) {
//        UserRank rank = userRankMapper.selectUserRankByUserId(userId);
//        if (rank == null) {
//            return null;
//        }
//        RankTab rankTab = rankTabMapper.selectByPrimaryKey(rank.getRankId());
        PageHelper.startPage(pageNum, pageSize, true);
        Page<UserRankMoreInfo> topRankUser = userRankMapper.selectTopRankUser(userId);
        List<UserRankMoreInfo> result = topRankUser.getResult();
        if (result == null) {
            return Collections.emptyList();
        } else {
            return result;
        }
    }

    @Override
    public List<UserRankMoreInfo> findTopRankUserSearch(String userId, String nickName, Integer pageNum, Integer pageSize) throws Exception {
        PageHelper.startPage(pageNum, pageSize, true);
        Page<UserRankMoreInfo> topRankUser = userRankMapper.selectTopRankUserSearch(userId,nickName);
        List<UserRankMoreInfo> result = topRankUser.getResult();
        if (result == null) {
            return Collections.emptyList();
        } else {
            return result;
        }
    }


    @Override
    public int doUserRankRelation(String fromUserId, String userId) {
        WxUser wxUser = wxUserMapper.selectById(fromUserId);
        if (wxUser == null) {
            throw new ParameterNullException("userId 为空");
        }
        WxUser toUser = wxUserMapper.selectById(userId);
        if (toUser == null) {
            throw new ParameterNullException("userId 为空");
        }

        Date date = new Date();

        List<String> has = userInviteMapper.checkUserInviteSingle(userId);
        if (CollectionUtils.isEmpty(has)) {
            UserInvite userInvite = new UserInvite();
            userInvite.setId(UUIDGenerator.generate());
            userInvite.setFromUser(fromUserId);
            userInvite.setToUser(userId);
            userInvite.setStates(0);
            userInvite.setCreateTime(date);
            userInvite.setUpdateTime(date);
            userInviteMapper.insert(userInvite);

        } else {
            if (has.contains(fromUserId)) {
                throw new ParameterNullException("该用户已是您的管家!");
            }
        }
        return 1;
    }

    @Override
    public UserRankBuy findUserRankBuy(String id) {
        UserRankBuy rankBuy = userRankBuyMapper.selectById(id);
        return rankBuy;
    }

    @Override
    public FatherInfo findUserFather(String userId) {
        FatherInfo info = new FatherInfo();
        List<String> father = userRankMapper.selectUserFather(userId);
        if (father != null && father.size() > 0) {
            info.setHas(true);
            UserRankInfo rankTabs = userRankMapper.selectUserRankTabs(father.get(0));
            info.setRankIcon(rankTabs.getRankIcon()).setRankName(rankTabs.getRankName())
                    .setRankNum(rankTabs.getRankNum()).setTopId(father.get(0))
                    .setTopNickName(rankTabs.getNickName()).setAvatar(rankTabs.getAvatar());
        }
        return info;
    }

    @Override
    public UserRankOutInfo findUserRankDetailInfo(String userId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize,true);
        Page<UserRankDetailInfo> pe = userRankMapper.selectUserRankUpInfo(userId);
        UserRankOutInfo outInfo = new UserRankOutInfo(pe.getPageNum(), pe.getPageSize(), pe.getStartRow(), pe.getEndRow(), pe.getTotal(), pe.getPages());
        List<UserRankDetailInfo> result = pe.getResult();
        outInfo.setChildren(result);
        return outInfo;
    }
}
